<template>
	<!-- 新契约工作列表模块-特别事项 -->
	<div class="fillcontain">
		<p style="padding: 15px 10px 0px;font-weight: bold;">核保综合结论</p>
		<div class="contain">
			<div v-for="(val,index) in resultList" :key="index">
        <span hidden :ref="'span' + index">{{val.name+":"+val.robotResultVos.length}}</span>
				<el-table :data="val.robotResultVos" :span-method="(param)=>objectSpanMethod(param,'span' + index)" :show-header="isShowHeader(index)">
					<!-- el-table无数据 -->
					<div slot="empty" style="height:0px">
					</div>
					<el-table-column label="险种类别" min-width="100" prop="name" show-overflow-tooltip></el-table-column>
					<el-table-column label="险种名称" min-width="100" prop="crtablHtml" show-overflow-tooltip></el-table-column>
					<el-table-column label="核保风险" min-width="120" prop="riskName" align='center' show-overflow-tooltip></el-table-column>
					<el-table-column label="智能点评" min-width="120">
						<template slot-scope="scope">
							<div v-for="(value,key) in scope.row.reviewBasisMapList" :key="key">{{value._sumItems}}</div>
						</template>
					</el-table-column>
					<el-table-column label="评点依据" min-width="80">
						<template slot-scope="scope">
							<div v-for="(value,key) in scope.row.reviewBasisMapList" :key="key">
                <div style="cursor: pointer;" @click="seeRevieDetail(value)" v-if="value.scaleMarksVOs && value.scaleMarksVOs.length">查看</div>
							</div>
						</template>
					</el-table-column>

					<el-table-column label="生成时间" min-width="120" prop="createTime" align='center' show-overflow-tooltip></el-table-column>
					<el-table-column label="综合结论" min-width="120" prop="comprehensiveComments" align='center' show-overflow-tooltip></el-table-column>
				</el-table>
			</div>
		</div>
		
    <p style="margin: 10px;font-weight: bold;" v-if="abnormalProductList.length">异常项目一览</p>
		<el-collapse v-model="activeNameList" v-if="abnormalProductList.length">
			<el-collapse-item :title="identity" name="1">
				<!-- 循环title -->
				<div v-for="(item,index) in abnormalProductList" :key="index">
					<!-- 如果title对应的有返回itemList则需要展示 -->
					<div>
						<div style="padding: 3px 10px;background-color: #ABDAFF;" v-if="item.parentName">{{item.parentName}}</div>
							<el-table :data="item.detalisChildPEVos" style="width:100%">
								<el-table-column min-width="80" property="name" label="名称" align='center' show-overflow-tooltip>
								</el-table-column>
								<el-table-column min-width="100" property="ret" label="结果" align='center' show-overflow-tooltip>
								</el-table-column>
								<el-table-column min-width="100" property="unit" label="单位" align='center' show-overflow-tooltip>
								</el-table-column>
								<el-table-column property="range" min-width="160" label="正常取值范围" align='center' show-overflow-tooltip>
								</el-table-column>
								<el-table-column min-width="80" label="是否异常" align='center' show-overflow-tooltip>
									<template slot-scope="scope">
											<span v-if="scope.row.isAbnormal == '1'" style="color: red;">异常</span>
											<span v-else>正常</span>
									</template>
								</el-table-column>
							</el-table>
					</div>
				</div>
			</el-collapse-item>
		</el-collapse>

		<p style="margin: 20px 10px 10px;font-weight: bold;">完整项目一览</p>
		<el-collapse v-model="activeNames" >
			<el-collapse-item :title="identity" name="1">
				<!-- 循环title -->
				<div v-for="(item,index) in unNormalProductList" :key="index">
					<!-- 如果title对应的有返回itemList则需要展示 -->
					<div>
						<div style="padding: 3px 10px;background-color: #ABDAFF;" v-if="item.parentName">{{item.parentName}}</div>
							<el-table :data="item.detalisChildPEVos" style="width:100%">
								<el-table-column min-width="80" property="name" label="名称" align='center' show-overflow-tooltip>
								</el-table-column>
								<el-table-column min-width="100" property="ret" label="结果" align='center' show-overflow-tooltip>
								</el-table-column>
								<el-table-column min-width="100" property="unit" label="单位" align='center' show-overflow-tooltip>
								</el-table-column>
								<el-table-column property="range" min-width="160" label="正常取值范围" align='center' show-overflow-tooltip>
								</el-table-column>
								<el-table-column min-width="80" label="是否异常" align='center' show-overflow-tooltip>
									<template slot-scope="scope">
											<span v-if="scope.row.isAbnormal == '1'" style="color: red;">异常</span>
											<span v-else>正常</span>
									</template>
								</el-table-column>
							</el-table>
					</div>
				</div>
			</el-collapse-item>
		</el-collapse>
		
		<!-- 弹框 -->
		<el-dialog title="评点依据" :visible.sync="opinionDialogVisible" width="70%" center>
			<el-row class="mb10">
				<el-table :data="reviewList">
					<el-table-column prop="itemId" label="ID" min-width="100" align='center' show-overflow-tooltip></el-table-column>
					<el-table-column prop="itemName" label="名称" min-width="200" align='center' show-overflow-tooltip></el-table-column>
					<el-table-column prop="itemCode" label="命中标记" min-width="200" align='center' show-overflow-tooltip></el-table-column>
					<el-table-column prop="range" align='center' min-width="200" label="范围描述" show-overflow-tooltip></el-table-column>
          <el-table-column prop="ret" label="本次检查结果" min-width="100" align='center' show-overflow-tooltip>
            <template slot-scope="scope">
							<span style="color: #d9001b">{{scope.row.ret}}</span>
						</template>
          </el-table-column>
					<el-table-column prop="fraction" align='center' min-width="100" label="评点分数" show-overflow-tooltip></el-table-column>
				</el-table>
			</el-row>
			<span slot="footer" class="dialog-footer">
				<el-button @click="opinionDialogVisible = false">返 回</el-button>
			</span>
		</el-dialog>
	</div>
</template>

<script>
  import { getPeDetailsApi } from "@/api/underwriting/uw";
	import constant from '@/utils/constant';
	export default {

		data() {
			return {
        activeNameList: ['1'],
				activeNames: ['1'],
        abnormalProductList: [],  //异常项目
				unNormalProductList:[],//投被保人归一数据
				serialNumber: '',
        identity: '投保人体检报告',
				resultList:[],
				opinionDialogVisible: false,
				/* 智能点评依据 */
				reviewList: [],
			}
		},
		created() {

		},
		activated() {
      let userInfo = JSON.parse(sessionStorage.getItem("userInfo"));
      let underwritingDetailInfo = sessionStorage.getItem('underwritingDetailInfo')
      if(underwritingDetailInfo){
        let params = {
          itemId: JSON.parse(underwritingDetailInfo).itemId,
	        itemNo: JSON.parse(underwritingDetailInfo).itemNo,
	        mainId: JSON.parse(underwritingDetailInfo).mainId,
	        peCode: JSON.parse(underwritingDetailInfo).peCode,
          underwritingFlow: JSON.parse(underwritingDetailInfo).underwritingFlow,
	        userId: userInfo ? userInfo.account : ''
        }
        /* let params = {
          "itemId": "59",
	        "itemNo": "a000033",
	        "mainId": "25",
	        "peCode": "20221202160520038671100000003",
	        "userId": userInfo.account
        } */
        this.queryPersonDetail(params)
      }
		},
		methods: {
			isShowHeader(index) {
				if (index >= 1) {
					return false;
				}
				return true;
			},

      // 核保综合结论-合并单元格
			objectSpanMethod({
				row,
				column,
				rowIndex,
				columnIndex,
			},El) {
        let rows = 0
        if(this.$refs[El]) {
          let spanEl = this.$refs[El][0].innerHTML
          rows = spanEl.substr(spanEl.indexOf(":") + 1);
        }
				if (columnIndex === 0 || columnIndex == 1 || columnIndex == 5 || columnIndex == 6) {
					if (rowIndex === 0) {
						return {
							rowspan: rows,
							colspan: 1
						}
					}
					return {
						rowspan: 0,
						colspan: 0
					}
				} else {
					return
				}
			},

      // 评点依据点击"查看"
			seeRevieDetail(array) {
        this.opinionDialogVisible = true;
				this.reviewList = array.scaleMarksVOs
			},
			exchangeUndefined(value, type) {
				if (value == 'null' || value == undefined || value == "" || value == null || value == "undefined") {
					if (type == 'high') {
						return "";
					} else {
						return "0";
					}
				}
				return value;
			},

      // 核保综合结论、异常项目一览查询
			queryPersonDetail(params) {
        this.$store.dispatch('loading/CHANGE_LOADING', true);
				this.resultList = [{
          robotResultVos: []
        }];
				this.unNormalProductList = [{
          detalisChildPEVos: []
        }];
        this.abnormalProductList = []
        getPeDetailsApi(params).then(res => {
          this.$store.dispatch('loading/CHANGE_LOADING', false);
					if (res.resultCode == constant.SUCCESS) {
            if(res.data.clientType == '2') this.identity = '被保人体检报告'
            else if(res.data.clientType == '3') this.identity = '投保人、被保人体检报告'
            // 异常项目一览
            if(res.data.detailsPeVoList && res.data.detailsPeVoList.length) {
              this.unNormalProductList = res.data.detailsPeVoList
              this.unNormalProductList.forEach(item=>{
                if(item.detalisChildPEVos?.length){
                  let filterArr = item.detalisChildPEVos.filter(fItem=>fItem.isAbnormal == '1')
                  if(filterArr.length) this.abnormalProductList.push({...item,detalisChildPEVos: filterArr})
                }
              })
            }
            // 综合结论
            if(!res.data.conclusionVos || !res.data.conclusionVos.length) return
            let newList = []
            res.data.conclusionVos.forEach(item=>{
              let obj = {
                name: item.crtableType,
                robotResultVos: []
              }
              if(item.riskVos && item.riskVos.length){
                item.riskVos.forEach(rItem=>{
                  if(obj.robotResultVos.length){
                    let ids = obj.robotResultVos.map(mItem=>mItem.diseaseId)
                    let _id = ids.indexOf(rItem.diseaseId)
                    if(_id > -1){
                      let test = rItem.sumItems || ''
                      if(rItem.sumItemsTag) test += rItem.sumItemsTag
                      obj.robotResultVos[_id].reviewBasisMapList.push({
                        _sumItems: test,
                        scaleMarksVOs: rItem.scaleMarksVOs
                      })
                      return
                    }
                  }
                  let test = rItem.sumItems || ''
                  if(rItem.sumItemsTag) test += rItem.sumItemsTag
                  obj.robotResultVos.push({
                    name: item.crtableType,
                    crtablHtml: item.crtableName,
                    riskName: rItem.underwritingRisk,
                    diseaseId: rItem.diseaseId,
                    createTime: item.createTime,
                    comprehensiveComments: item.conclusion,
                    reviewBasisMapList: [{
                      _sumItems: test,
                      scaleMarksVOs: rItem.scaleMarksVOs
                    }]
                  })
                })
              }else{
                obj.robotResultVos.push({
                  name: item.crtableType,
                  crtablHtml: item.crtableName,
                  createTime: item.createTime,
                  comprehensiveComments: item.conclusion,
                  reviewBasisMapList: []
                })
              }
              newList.push(obj)
            })

            this.resultList = newList
					}
				}).catch(err => {
				this.$store.dispatch('loading/CHANGE_LOADING', false);
			})
			},
		},
		computed: {},

	}
</script>

<style lang="less" scoped>
	.el-table {
		td {
			border: 1px solid #EBEEF5 !important;
		}
	}

	.el-table {
		td {
			is-leaf{
				border: 1px solid #EBEEF5 !important;
			}
		}
		th {
			is-leaf{
				border: 1px solid #EBEEF5 !important;
			}
		}
	}

	.fillcontain {
		padding: 0 10px 10px;
    min-height: calc(100vh - 200px);

		.fastQuery {
			padding: 15px 20px 10px 20px;
			background: #fff;
			border-bottom: 1px solid #e1e1e1;

			.fastQuery_title {
				color: #666;
				padding: 6px 0px;
			}

			.fastQuery_item {
				margin: 0 10px;
			}

			.item_default {
				.el-button {
					color: #409EFF !important;
					border-color: #c6e2ff !important;
					background-color: #ecf5ff !important;
				}

			}

			.moreCondition {
				color: #68a3fc;
				cursor: pointer;
				padding: 6px 0px;

				.el-icon-d-arrow-right {
					transform: rotate(90deg);
				}

				.el-icon-d-arrow-left {
					transform: rotate(-90deg);
				}
			}
		}
	}

	.contain {
		background: #fff;
		padding: 10px 0;
		margin-bottom: 20px;
	}

	.pagination {
		padding: 10px 20px;
		text-align: right;
	}

	.operationIcon {
		// padding: 12px;
		border: 1px solid #e5e5e5;
		border-radius: 50%;
		width: 22px;
		height: 22px;
		display: inline-block;
		cursor: pointer;
		box-shadow: 0 0 3px #f9f9f9;

		.svg-icon {
			font-size: 14px;
			fill: #68a3fc;
		}
	}

	.search_container {
		padding: 20px 20px 0 20px;
	}

	.el-dropdown {
		vertical-align: top;
	}

	.el-dropdown+.el-dropdown {
		margin-left: 15px;
	}

	.el-icon-arrow-down {
		font-size: 12px;
	}
</style>
